package com.china.lottery.listener.jczq;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.blogjava.frankiegao123.log.slf4j.Log;
import net.blogjava.frankiegao123.log.slf4j.LogFactory;
import net.sf.json.JSONObject;

import org.json.JSONArray;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.china.lottery.listener.dao.TbSpJczqDao;
import com.china.lottery.listener.dao.lChannelSmgTournamentbakDao;
import com.china.lottery.listener.dao.entity.TB_SP_JCZQ;
import com.china.lottery.listener.dao.entity.lChannelSmgTournamentbak;
import com.china.lottery.listener.jczq.util.JinCaiGrepUtil;
import com.china.lottery.listener.jczq.util.JinCaiStringUtil;
import com.china.lottery.listener.util.DateToWeekToDateUtil;
import com.mina.rbc.util.CheckUtil;
import com.mina.rbc.util.DateUtil;
import com.mina.rbc.util.StringUtil;

public class JczqGrepEngine {

	protected Log log = LogFactory.getLog(getClass());

	@Autowired
	private TbSpJczqDao dao;
	@Autowired
	private lChannelSmgTournamentbakDao lchannelsmgtournamentbakdao;

	/**
	 * 抓取赔率，变化 http://info.sporttery.cn/football/hhad_list.php
	 * 
	 * @param dao
	 * @throws Exception
	 */
	public void grepGuDingData(TbSpJczqDao dao) throws Exception {
		
		String url = "http://info.sporttery.cn/interface/interface_mixed.php?action=fb_list&pke=0.1726121063993522&_=1370308305647";

		String tmp = JinCaiGrepUtil.fetchDataFromURL(url, "GBK", 30);
		Pattern pattern = Pattern.compile("var data=([^;]*?);");
		Matcher matcher = pattern.matcher(tmp);
		if (!matcher.find()) {
			return;
		}

		String data = matcher.group(1);
		JSONArray array = new JSONArray(data);
		for (int i = 0; i < array.length(); i++) {
			JSONArray match = array.getJSONArray(i);
			JSONArray matchinfo = match.getJSONArray(0);
			String sItemID = matchinfo.getString(0);
			String mt = JinCaiStringUtil.convert2MatchTime(
					matchinfo.getString(3), 0);
			Date dmt = DateUtil.parserDateTime(mt);
			dmt.setTime(dmt.getTime() - 1000 * 60 * 60 * 24 * 1);
			String itemID = JinCaiStringUtil.convertItemNameToID(dmt, sItemID);// 当天时间组成

			String matchHost = matchinfo.getString(2);
			String[] ss = StringUtil.splitter(matchHost, "$");
			String s1 = ss[1];
			String team = ss[0] + " vs " + ss[2];

			// 让球胜平负 3种
			JSONArray rspf_sp_arr = match.getJSONArray(1);
			String rspf = "";
			if (rspf_sp_arr.length() > 0) {
				JSONObject maprspf = new JSONObject();
				maprspf.put("letvs.let", s1);
				maprspf.put("letvs.v3", rspf_sp_arr.get(0));
				maprspf.put("letvs.v1", rspf_sp_arr.get(1));
				maprspf.put("letvs.v0", rspf_sp_arr.get(2));
				rspf = maprspf.toString();
			} else {
				//赛事取消的赔率设置为0
				rspf = "0";  
			}

			// 胜平负 3种
			JSONArray spf_sp_arr = match.getJSONArray(5);
			String spf = "";
			if (spf_sp_arr.length() > 0) {
				JSONObject mapspf = new JSONObject();
				mapspf.put("vs.v3", spf_sp_arr.get(0));
				mapspf.put("vs.v1", spf_sp_arr.get(1));
				mapspf.put("vs.v0", spf_sp_arr.get(2));
				spf = mapspf.toString();
			} else {
				//赛事取消的赔率设置为0
				spf = "0";
			}
			// 比分 31种
			JSONArray spf_cbf_arr = match.getJSONArray(2);
			String cbf = "";
			if (spf_cbf_arr.length() > 0) {
				JSONObject mapcbf = new JSONObject();
				mapcbf.put("score.v10", spf_cbf_arr.get(0));
				mapcbf.put("score.v20", spf_cbf_arr.get(1));
				mapcbf.put("score.v21", spf_cbf_arr.get(2));
				mapcbf.put("score.v30", spf_cbf_arr.get(3));
				mapcbf.put("score.v31", spf_cbf_arr.get(4));
				mapcbf.put("score.v32", spf_cbf_arr.get(5));
				mapcbf.put("score.v40", spf_cbf_arr.get(6));
				mapcbf.put("score.v41", spf_cbf_arr.get(7));
				mapcbf.put("score.v42", spf_cbf_arr.get(8));
				mapcbf.put("score.v50", spf_cbf_arr.get(9));
				mapcbf.put("score.v51", spf_cbf_arr.get(10));
				mapcbf.put("score.v52", spf_cbf_arr.get(11));
				mapcbf.put("score.v90", spf_cbf_arr.get(12));// 胜其他 v10 代表 1:0

				mapcbf.put("score.v00", spf_cbf_arr.get(13));
				mapcbf.put("score.v11", spf_cbf_arr.get(14));
				mapcbf.put("score.v22", spf_cbf_arr.get(15));
				mapcbf.put("score.v33", spf_cbf_arr.get(16));
				mapcbf.put("score.v99", spf_cbf_arr.get(17));// 平其他

				mapcbf.put("score.v01", spf_cbf_arr.get(18));
				mapcbf.put("score.v02", spf_cbf_arr.get(19));
				mapcbf.put("score.v12", spf_cbf_arr.get(20));
				mapcbf.put("score.v03", spf_cbf_arr.get(21));
				mapcbf.put("score.v13", spf_cbf_arr.get(22));
				mapcbf.put("score.v23", spf_cbf_arr.get(23));
				mapcbf.put("score.v04", spf_cbf_arr.get(24));
				mapcbf.put("score.v14", spf_cbf_arr.get(25));
				mapcbf.put("score.v24", spf_cbf_arr.get(26));
				mapcbf.put("score.v05", spf_cbf_arr.get(27));
				mapcbf.put("score.v15", spf_cbf_arr.get(28));
				mapcbf.put("score.v25", spf_cbf_arr.get(29));
				mapcbf.put("score.v09", spf_cbf_arr.get(30));// 负其他

				cbf = mapcbf.toString();

			} else {
				//赛事取消的赔率设置为0
				cbf = "0";
			}

			// 总进球 7种
			JSONArray spf_jqs_arr = match.getJSONArray(3);
			String jqs = "";
			if (spf_jqs_arr.length() > 0) {

				JSONObject mapjqs = new JSONObject();

				mapjqs.put("goal.v0", spf_jqs_arr.get(0));
				mapjqs.put("goal.v1", spf_jqs_arr.get(1));
				mapjqs.put("goal.v2", spf_jqs_arr.get(2));
				mapjqs.put("goal.v3", spf_jqs_arr.get(3));
				mapjqs.put("goal.v4", spf_jqs_arr.get(4));
				mapjqs.put("goal.v5", spf_jqs_arr.get(5));
				mapjqs.put("goal.v6", spf_jqs_arr.get(6));
				mapjqs.put("goal.v7", spf_jqs_arr.get(7));

				jqs = mapjqs.toString();
			} else {
				//赛事取消的赔率设置为0
				jqs = "0";
			}
			// 半全场胜平负 310 胜平负 9种
			JSONArray spf_bqc_arr = match.getJSONArray(4);
			String bqc = "";
			if (spf_bqc_arr.length() > 0) {
				JSONObject mapbqc = new JSONObject();
				mapbqc.put("half.v33", spf_bqc_arr.get(0));
				mapbqc.put("half.v31", spf_bqc_arr.get(1));
				mapbqc.put("half.v30", spf_bqc_arr.get(2));

				mapbqc.put("half.v13", spf_bqc_arr.get(3));
				mapbqc.put("half.v11", spf_bqc_arr.get(4));
				mapbqc.put("half.v10", spf_bqc_arr.get(5));

				mapbqc.put("half.v03", spf_bqc_arr.get(6));
				mapbqc.put("half.v01", spf_bqc_arr.get(7));
				mapbqc.put("half.v00", spf_bqc_arr.get(8));

				bqc = mapbqc.toString();
			} else {
				//赛事取消的赔率设置为0
				bqc = "0";
			}
			// 足球6种 :
			// 胜平负(20201),比分(20202),总进球(20203),半场胜平负(20204),混合串关玩法(20205),让分胜平负(20206)
			// spf = spf.substring(0, spf.length() - 1);// 胜平负 1.57,3.35,4.50
			// rspf = rspf.substring(0, rspf.length() - 1);// 让球胜平负
			// 2.82,3.40,1.96
			// cbf = cbf.substring(0, cbf.length() - 1);// 比分
			// 5.80,6.50,7.00,10.50,13.00,24.00,23.00,28.00,60.00,60.00,70.00,150.0,50.00,9.00,6.00,16.00,70.00,500.0,10.50,23.00,13.00,70.00,39.00,39.00,250.0,150.0,200.0,700.0,600.0,600.0,250.0
			// jqs = jqs.substring(0, jqs.length() - 1);// 总进球数
			// 9.00,4.00,3.15,3.60,5.70,10.50,19.00,29.00
			// bqc = bqc.substring(0, bqc.length() - 1);// 半全场胜平负
			// 2.12,17.00,50.00,4.10,5.10,10.00,26.00,17.00,8.00
			// 胜平负
			List<?> list = dao.getTbSpJcZqByNameAndDateAndCgameId("胜平负",
					itemID, sItemID);
			if (list != null && list.size() != 0) {
				Object[] obj = (Object[]) list.get(0);
				String cgameids = (String) obj[1];
				String citemids = (String) obj[2]; // 比率待开的情况 --
				String sp = (String) obj[4];
				if (!sp.equals(spf) && cgameids.equals(sItemID)
						&& citemids.equals(itemID)) {
					log.info("抓取变化后赔率,胜平负:期次："+citemids+",spf:"+spf);

					TB_SP_JCZQ t1 = new TB_SP_JCZQ();
					t1.setcGameId(sItemID);
					t1.setcItemId(itemID);
					t1.setcAddDate(dmt);
					t1.setcValue(spf);
					t1.setcName("胜平负");
					t1.setCtype("20201");
					t1.setCvs(team);
					Date day = new Date();
					t1.setcIndate(day);
					dao.save(t1);
				}
			} else {
				log.info("抓取赔率,胜平负:期次："+itemID+",spf:"+spf);

				TB_SP_JCZQ t1 = new TB_SP_JCZQ();
				t1.setcGameId(sItemID);
				t1.setcItemId(itemID);
				t1.setcAddDate(dmt);
				t1.setcValue(spf);
				t1.setcName("胜平负");
				t1.setCtype("20201");
				t1.setCvs(team);
				Date day = new Date();
				t1.setcIndate(day);
				dao.save(t1);
			}
			// 让球胜平负
			List listr = dao.getTbSpJcZqByNameAndDateAndCgameId("让球胜平负",
					itemID, sItemID);
			if (listr != null && listr.size() != 0) {
				Object[] obj = (Object[]) listr.get(0);
				String cgameids = (String) obj[1];
				String citemids = (String) obj[2];
				String sp = (String) obj[4];
				if (!sp.equals(rspf) && cgameids.equals(sItemID)
						&& citemids.equals(itemID)) {
					log.info("抓取变化后赔率,让球胜平负:期次："+citemids+",rspf:"+rspf);

					TB_SP_JCZQ t2 = new TB_SP_JCZQ();
					t2.setcGameId(sItemID);
					t2.setcItemId(itemID);
					t2.setcAddDate(dmt);
					t2.setcValue(rspf);
					t2.setcName("让球胜平负");
					t2.setCtype("20206");
					t2.setCvs(team);
					Date day = new Date();
					t2.setcIndate(day);
					dao.save(t2);
				}
			} else {
				log.info("抓取赔率,让球胜平负:期次："+itemID+",rspf:"+rspf);

				TB_SP_JCZQ t2 = new TB_SP_JCZQ();
				t2.setcGameId(sItemID);
				t2.setcItemId(itemID);
				t2.setcAddDate(dmt);
				t2.setcValue(rspf);
				t2.setCtype("20206");
				t2.setCvs(team);
				t2.setcName("让球胜平负");
				Date day = new Date();
				t2.setcIndate(day);
				dao.save(t2);
			}
			// 比分
			List<?> listb = dao.getTbSpJcZqByNameAndDateAndCgameId("比分",
					itemID, sItemID);
			if (listb != null && listb.size() != 0) {
				Object[] obj = (Object[]) listb.get(0);
				String cgameids = (String) obj[1];
				String citemids = (String) obj[2];
				String sp = (String) obj[4];
				if (!sp.equals(cbf) && cgameids.equals(sItemID)
						&& citemids.equals(itemID)) {
					log.info("抓取变化后赔率,比分:期次："+citemids+",cbf:"+cbf);
					TB_SP_JCZQ t3 = new TB_SP_JCZQ();
					t3.setcGameId(sItemID);
					t3.setcItemId(itemID);
					t3.setcAddDate(dmt);
					t3.setcValue(cbf);
					t3.setcName("比分");
					t3.setCtype("20202");
					t3.setCvs(team);
					Date day = new Date();
					t3.setcIndate(day);
					dao.save(t3);
				}
			} else {
				log.info("抓取赔率,比分:期次："+itemID+",cbf:"+cbf);

				TB_SP_JCZQ t3 = new TB_SP_JCZQ();
				t3.setcGameId(sItemID);
				t3.setcItemId(itemID);
				t3.setcAddDate(dmt);
				t3.setcValue(cbf);
				t3.setcName("比分");
				t3.setCtype("20202");
				t3.setCvs(team);
				Date day = new Date();
				t3.setcIndate(day);
				dao.save(t3);
			}

			// 总进球数
			List<?> listz = dao.getTbSpJcZqByNameAndDateAndCgameId("总进球数",
					itemID, sItemID);
			if (listz != null && listz.size() != 0) {
				Object[] obj = (Object[]) listz.get(0);
				String cgameids = (String) obj[1];
				String citemids = (String) obj[2];
				String sp = (String) obj[4];
				if (!sp.equals(jqs) && cgameids.equals(sItemID)
						&& citemids.equals(itemID)) {
					log.info("抓取变化后赔率,总进球数:期次："+citemids+",jqs:"+jqs);

					TB_SP_JCZQ t4 = new TB_SP_JCZQ();
					t4.setcGameId(sItemID);
					t4.setcItemId(itemID);
					t4.setcAddDate(dmt);
					t4.setcValue(jqs);
					t4.setcName("总进球数");
					t4.setCtype("20203");
					t4.setCvs(team);
					Date day = new Date();
					t4.setcIndate(day);
					dao.save(t4);
				}
			} else {
				log.info("抓取赔率,总进球数:期次："+itemID+",jqs:"+jqs);

				TB_SP_JCZQ t4 = new TB_SP_JCZQ();
				t4.setcGameId(sItemID);
				t4.setcItemId(itemID);
				t4.setcAddDate(dmt);
				t4.setcValue(jqs);
				t4.setcName("总进球数");
				t4.setCtype("20203");
				t4.setCvs(team);
				Date day = new Date();
				t4.setcIndate(day);
				dao.save(t4);
			}

			// 半全场胜平负
			List<?> listban = dao.getTbSpJcZqByNameAndDateAndCgameId("半全场胜平负",
					itemID, sItemID);
			if (listban != null && listban.size() != 0) {
				Object[] obj = (Object[]) listban.get(0);
				String cgameids = (String) obj[1];
				String citemids = (String) obj[2];
				String sp = (String) obj[4];
				if (!sp.equals(bqc) && cgameids.equals(sItemID)
						&& citemids.equals(itemID)) {
					log.info("抓取变化后赔率,半全场胜平负:期次："+citemids+",bqc:"+bqc);

					TB_SP_JCZQ t5 = new TB_SP_JCZQ();
					t5.setcGameId(sItemID);
					t5.setcItemId(itemID);
					t5.setcAddDate(dmt);
					t5.setcValue(bqc);
					t5.setcName("半全场胜平负");
					t5.setCtype("20204");
					t5.setCvs(team);
					Date day = new Date();
					t5.setcIndate(day);
					dao.save(t5);
				}
			} else {
				log.info("抓取赔率,半全场胜平负:期次："+itemID+",bqc:"+bqc);

				TB_SP_JCZQ t5 = new TB_SP_JCZQ();
				t5.setcGameId(sItemID);
				t5.setcItemId(itemID);
				t5.setcAddDate(dmt);
				t5.setcValue(bqc);
				t5.setcName("半全场胜平负");
				t5.setCtype("20204");
				t5.setCvs(team);
				Date day = new Date();
				t5.setcIndate(day);
				dao.save(t5);
			}
		}
	}

	// 获取插入赛果
	public void grepPeriod(
			lChannelSmgTournamentbakDao lchannelsmgtournamentbakdao)
			throws Exception {
		String url = "http://info.sporttery.cn/interface/interface_mixed.php?action=fb_list&pke=0.1726121063993522&_=1370308305647";
		String tmp = JinCaiGrepUtil.fetchDataFromURL(url, "GBK", 30);
		Pattern pattern = Pattern.compile("var data=([^;]*?);");
		Matcher matcher = pattern.matcher(tmp);
		if (!matcher.find()) {
			return;
		}
		String data = matcher.group(1);
		JSONArray array = new JSONArray(data);
		for (int i = 0; i < array.length(); i++) {
			JSONArray match = array.getJSONArray(i);
			JSONArray matchinfo = match.getJSONArray(0);
			String sItemID = matchinfo.getString(0);
			String mt = JinCaiStringUtil.convert2MatchTime(
					matchinfo.getString(3), 0);
			Date dmt = DateUtil.parserDateTime(mt);
			dmt.setTime(dmt.getTime() - 1000 * 60 * 60 * 24 * 1);
			String itemID = JinCaiStringUtil.convertItemNameToID(dmt, sItemID);// 当天时间组成
			// sItemID); //开赛时间组成
			lChannelSmgTournamentbak l = new lChannelSmgTournamentbak();
			String smgDay = itemID.substring(0, 8);
			String weekId = itemID.substring(8, 9);
			String teamId = itemID.substring(9);
			List list = lchannelsmgtournamentbakdao
					.getLcSmgTouBakBySmgdayAndWeekIdAndTeamId(smgDay, weekId,
							teamId);
			if (list == null || list.size() == 0) {
				String league = matchinfo.getString(1);
				// String team=matchinfo.getString(2);
				l.setSmgDay(smgDay);
				l.setWeekId(weekId);
				l.setTeamId(teamId);
				l.setLeague(league);
				String matchHost = matchinfo.getString(2);
				String[] ss = StringUtil.splitter(matchHost, "$");
				if (!CheckUtil.isNullString(ss[1])) {
					String s1 = StringUtil.replaceString(ss[1], "+", "");
					l.setLet(s1);
				}

				String s1 = ss[1];
				String team = ss[0] + ":" + ss[2];
				String endTimes1 = matchinfo.getString(3); // 18-07-04 18:00
				String endTimes2 = endTimes1.substring(2);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
				Date ds = new Date();
				String year = sdf.format(ds);
				String endTime = year + endTimes2 + ":00"; // 2018-07-04
				SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date de = f.parse(endTime); // Wed Jul 04 18:00:00 CST 2018
				String s4 = "";
				Date datee = null;
				// 周一至周五 24:00-9:00 周六，周日1：00-9:00 这期间停售，这之间开赛的时间需要提前至售票前
				if (weekId.equals("1") || weekId.equals("2")
						|| weekId.equals("3") || weekId.equals("4")
						|| weekId.equals("5")) {
					String s = endTime.substring(11, 13);
					if (s.equals("00") || s.equals("01") || s.equals("02")
							|| s.equals("03") || s.equals("04")
							|| s.equals("05") || s.equals("06")
							|| s.equals("07") || s.equals("08")) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(de);
						cal.add(Calendar.DATE, -1); // 减1天
						String s2 = f.format(cal.getTime());// 2018-07-03
						String date = s2.substring(8, 10);// 03
						String s3 = s2.substring(0, 11);
						s4 = s3 + "24:00:00";
						// Wed Jul 04 18:00:00 CST 2018
						datee = new Date(f.parse(s4).getTime() - 1800000); // -40分//减1天

					} else {

						datee = new Date(f.parse(endTime).getTime() - 1800000); // -40分
					}

				} else {
					String s = endTime.substring(11, 13);
					if (s.equals("01") || s.equals("02") || s.equals("03")
							|| s.equals("04") || s.equals("05")
							|| s.equals("06") || s.equals("07")
							|| s.equals("08")) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(de);
						cal.add(Calendar.DATE, -1); // 减1天
						// System.out.println(f.format(cal.getTime()));
						String s2 = f.format(cal.getTime());// 2018-07-03
						String date = s2.substring(8, 10);// 03
						String s3 = s2.substring(0, 11);
						s4 = s3 + "24:00:00";
						// Wed Jul 04 18:00:00 CST 2018
						datee = new Date(f.parse(s4).getTime() - 1800000); // -40分//减1天

					} else {

						datee = new Date(f.parse(endTime).getTime() - 1800000); // -40分
					}

				}

				Date endtime = f.parse(endTime);
				Date d = new Date();
				int a = datee.compareTo(d);
				String flag = "";
				if (a > 0) {
					flag = "0"; // 销售
				} else if (a < 0) {
					flag = "1"; // 停售
				} else {
					flag = "3"; // 截止状态
				}

				String smgType = "1"; // 0 1 篮球或足球
				String text1 = itemID;
				String gameName = "";
				l.setLet(null);
				l.setTeam(team);
				l.setMatchTime(f.format(datee));// -9
				l.setEndTime(f.format(datee));//
				l.setFlag(flag);
				l.setSmgType(smgType);
				l.setText1(text1);
				l.setGameName(null);
				l.setCancel(null);// 进行中
				Date date = new Date();
				l.setCreateTime(date);
				log.info("插入赛事,赛事编号为: " + text1);
				lchannelsmgtournamentbakdao.insert(l);
			}
		}
	}

	// 更新赛果
	public void grepResult(
			lChannelSmgTournamentbakDao lchannelsmgtournamentbakdao)
			throws Exception {
		for (int i = 1; i < 1000; i++) {
			Date day = new Date();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String end_date = df.format(day); // 当天时间
			Calendar begin = Calendar.getInstance();
			begin.setTime(day);
			begin.add(Calendar.DAY_OF_MONTH, -2);
			String start_date = df.format(begin.getTime());// 三天前
			String URL = "http://info.sporttery.cn/football/match_result.php?page="
					+ i
					+ "&search_league=0&start_date="
					+ start_date
					+ "&end_date=" + end_date + "&dan=";
			// String URL =
			// "http://info.sporttery.cn/football/match_result.php";
			String datas = JinCaiGrepUtil.fetchDataFromURL(URL, "GBK", 30);
			Document doc = Jsoup.parse(datas);
			Elements links = doc.getElementsByTag("table");
			if (links != null && links.size() > 2) {
				Element link = links.get(1); // 获取第二个table
				Elements eltr = link.getElementsByTag("tr");
				int trs = eltr.size();// 本来一页长度30+2非记录；
				if (trs < 32) {
					i = 1000;
				}
				for (Element tr : eltr) {
					String smgDay = "";
					String weekId = "";
					String teamId = "";
					Elements el = tr.select("td");
					if (el != null && el.size() > 10) {
						String cancel = tr.select("td").get(9).text();// 状态
						String smgDays = tr.select("td").get(0).text();
						String cgameid = tr.select("td").get(1).text();
						// smgDay=smgDays.replaceAll("-", "");
						String str = DateToWeekToDateUtil.getWeek(smgDays,
								cgameid);
						smgDay = DateToWeekToDateUtil.DateToString(str);

						String w = tr.select("td").get(1).text();
						String week = w.substring(0, 2);
						switch (week) {
						case "周一":
							weekId = "1";
							break;
						case "周二":
							weekId = "2";
							break;
						case "周三":
							weekId = "3";
							break;
						case "周四":
							weekId = "4";
							break;
						case "周五":
							weekId = "5";
							break;
						case "周六":
							weekId = "6";
							break;
						default:
							weekId = "7";
						}

						teamId = w.substring(2);

						String ss = tr.select("span").get(0).text();// 上半场比分
						String nn = ss.substring(ss.indexOf("("),
								ss.indexOf(")") + 1);
						String let = nn.substring(1, nn.length() - 1);// 让球
						if (let.contains("+")) {
							let = let.replace("+", "");
						}

						String firsthalfresult = tr.select("span").get(3)
								.text();// 上半场比分
						String result = tr.select("span").get(4).text();// 比分

						List l = lchannelsmgtournamentbakdao
								.getLcSmgTouBakBySmgdayAndWeekIdAndTeamId(
										smgDay, weekId, teamId);
						if (l != null && l.size() != 0) {
							Date dates = new Date();
							Object[] o = (Object[]) l.get(0);
							String endtime = (String) o[6];

							if (endtime.substring(10, 11).equals("T")) {
								endtime = endtime.replace("T", " ");
							}

							String flags = (String) o[8];
							SimpleDateFormat f = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							Date endTime = f.parse(endtime);
							int a = endTime.compareTo(dates);
							String flag = "";
							if (a < 0 || a == 0 && !flags.equals("1")) { // 停售
								flag = "1";
								lchannelsmgtournamentbakdao.updateToFlag(flag,
										smgDay, weekId, teamId);
							} else if (a > 0 && !flags.equals("0")) {
								flag = "0"; // 销售
								lchannelsmgtournamentbakdao.updateToFlag(flag,
										smgDay, weekId, teamId);
							}
						}

						if (cancel != null && cancel.equals("已完成")) { // 更新
							List list = lchannelsmgtournamentbakdao
									.getLcSmgTouBakBySmgdayAndWeekIdAndTeamId(
											smgDay, weekId, teamId);
							String cancels =null;
							String resul = null;
							String fresul = null;

							if (list != null && list.size() != 0) {
								Object[] obj = (Object[]) list.get(0);
								cancels = (String) obj[19];
								resul = (String) obj[20];
								fresul = (String) obj[21];


								String lets = (String) obj[22];
								if (lets == null || lets == "") {
									log.info("官网赛果已完成,赛事编号为：" + smgDay + weekId+ teamId+"官网赛果已完成，更新让球："+let);
									System.out.println("官网赛果已完成,赛事编号为：" + smgDay + weekId+ teamId+"官网赛果已完成，更新让球："+let);

									lchannelsmgtournamentbakdao.updateToLet(
											let, smgDay, weekId, teamId);
								}
								if(cancels==null){
									cancels="0";
								}
								if (cancels.equals("2")||(cancels.equals("0")&&resul==null||fresul==null)) {//改
									log.info("官网赛果已完成，更新赛果，赛事编号为：" + smgDay + weekId
											+ teamId+"上半场比分firsthalfresult:"+firsthalfresult+",全场比分result:"+result);
									Date date = new Date();
									lchannelsmgtournamentbakdao.update("0",
											firsthalfresult, result, smgDay,
											weekId, teamId, date, "0");
								}
							}
						} else if (cancel.equals("已取消")) {// 已取消
							List list = lchannelsmgtournamentbakdao
									.getLcSmgTouBakBySmgdayAndWeekIdAndTeamId(
											smgDay, weekId, teamId);
							String cancels = "";
							if (list != null && list.size() != 0) {
								int size = list.size();
								Object[] obj = (Object[]) list.get(0);
								cancels = (String) obj[19];
								String lets = (String) obj[22];
								if (lets == null || lets == "") {
									log.info("比赛取消,期次："+smgDay+weekId+teamId+"赛事取消，更新让球："+let);
									lchannelsmgtournamentbakdao.updateToLet(
											let, smgDay, weekId, teamId);
								}
								if (cancels==null) {
									log.info("比赛取消,期次："+smgDay+weekId+teamId);
									Date dates = new Date();
									lchannelsmgtournamentbakdao.update("2",
											firsthalfresult, result, smgDay,
											weekId, teamId, dates, "0");
								}
							}

						}
					}
				}
			}
		}
	}
}
